"""create users and refresh_tokens tables from auth module.

Revision ID: f13f11d01b9e
Revises: 
Create Date: 2024-09-04 11:29:30.357160

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'f13f11d01b9e'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('auth_users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(length=64), nullable=False),
    sa.Column('email', sa.String(length=120), nullable=False),
    sa.Column('password_hash', sa.String(length=256), nullable=True),
    sa.Column('status_id', sa.SmallInteger(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_auth_users_email'), 'auth_users', ['email'], unique=True)
    op.create_index(op.f('ix_auth_users_id'), 'auth_users', ['id'], unique=False)
    op.create_index(op.f('ix_auth_users_status_id'), 'auth_users', ['status_id'], unique=False)
    op.create_index(op.f('ix_auth_users_username'), 'auth_users', ['username'], unique=True)
    op.create_table('auth_refresh_tokens',
    sa.Column('token', sa.String(length=24), nullable=False),
    sa.Column('hash', sa.String(length=255), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('expires_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['auth_users.id'], ),
    sa.PrimaryKeyConstraint('token')
    )
    op.create_index(op.f('ix_auth_refresh_tokens_expires_at'), 'auth_refresh_tokens', ['expires_at'], unique=False)
    op.create_index(op.f('ix_auth_refresh_tokens_user_id'), 'auth_refresh_tokens', ['user_id'], unique=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_auth_refresh_tokens_user_id'), table_name='auth_refresh_tokens')
    op.drop_index(op.f('ix_auth_refresh_tokens_expires_at'), table_name='auth_refresh_tokens')
    op.drop_table('auth_refresh_tokens')
    op.drop_index(op.f('ix_auth_users_username'), table_name='auth_users')
    op.drop_index(op.f('ix_auth_users_status_id'), table_name='auth_users')
    op.drop_index(op.f('ix_auth_users_id'), table_name='auth_users')
    op.drop_index(op.f('ix_auth_users_email'), table_name='auth_users')
    op.drop_table('auth_users')
    # ### end Alembic commands ###
